package com.video.video.service.impl;

import com.video.common.utils.uuid.IdUtils;
import com.video.video.domain.DUser;
import com.video.video.domain.dto.DWthdrawalRecordDto;
import com.video.video.domain.vo.DWthdrawalRecordQuery;
import com.video.video.service.IDUserService;
import java.util.List;
import com.video.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.video.video.mapper.DWthdrawalRecordMapper;
import com.video.video.domain.DWthdrawalRecord;
import com.video.video.service.IDWthdrawalRecordService;

/**
 * 提现记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-27
 */
@Service
public class DWthdrawalRecordServiceImpl implements IDWthdrawalRecordService
{
    @Autowired
    private DWthdrawalRecordMapper dWthdrawalRecordMapper;
    @Autowired
    private IDUserService idUserService;

    /**
     * 查询提现记录
     *
     * @param id 提现记录主键
     * @return 提现记录
     */
    @Override
    public DWthdrawalRecord selectDWthdrawalRecordById(String id)
    {
        return dWthdrawalRecordMapper.selectDWthdrawalRecordById(id);
    }

    /**
     * 查询提现记录列表
     *
     * @param query 提现记录
     * @return 提现记录
     */
    @Override
    public List<DWthdrawalRecordDto> selectDWthdrawalRecordList(DWthdrawalRecordQuery query)
    {
        return dWthdrawalRecordMapper.selectDWthdrawalRecordList(query);
    }

    /**
     * 新增提现记录
     *
     * @param dWthdrawalRecord 提现记录
     * @return 结果
     */
    @Override
    public int insertDWthdrawalRecord(DWthdrawalRecord dWthdrawalRecord)
    {
        DUser huiyuan= idUserService.selectDUserById(dWthdrawalRecord.getUid());
        dWthdrawalRecord.setId(IdUtils.getId());
        dWthdrawalRecord.setCurrentAmount(huiyuan.getCommissionBalance());
        dWthdrawalRecord.setCreateTime(DateUtils.getNowDate());
        int flg=dWthdrawalRecordMapper.insertDWthdrawalRecord(dWthdrawalRecord);
        if(flg > 0){
            //申请提现直接扣减返佣余额
            huiyuan.setCommissionBalance(huiyuan.getCommissionBalance().subtract(dWthdrawalRecord.getAmount()));
            idUserService.updateDUserInfo(huiyuan);
        }
        return flg;
    }

    /**
     * 修改提现记录
     *
     * @param dWthdrawalRecord 提现记录
     * @return 结果
     */
    @Override
    public int updateDWthdrawalRecord(DWthdrawalRecord dWthdrawalRecord)
    {

        DUser huiyuan= idUserService.selectDUserById(dWthdrawalRecord.getUid());
        //驳回-将提现金额返回会员返佣余额
        if(dWthdrawalRecord.getAudiStatus().equals("3")){
            huiyuan.setCommissionBalance(huiyuan.getCommissionBalance().add(dWthdrawalRecord.getAmount()));
            idUserService.updateDUserInfo(huiyuan);
        }
        //审核通过修改状态（提现申请已扣减会员返佣余额）
        dWthdrawalRecord.setUpdateTime(DateUtils.getNowDate());
        return dWthdrawalRecordMapper.updateDWthdrawalRecord(dWthdrawalRecord);
    }

    /**
     * 批量删除提现记录
     *
     * @param ids 需要删除的提现记录主键
     * @return 结果
     */
    @Override
    public int deleteDWthdrawalRecordByIds(String[] ids)
    {
        return dWthdrawalRecordMapper.deleteDWthdrawalRecordByIds(ids);
    }

    /**
     * 删除提现记录信息
     *
     * @param id 提现记录主键
     * @return 结果
     */
    @Override
    public int deleteDWthdrawalRecordById(String id)
    {
        return dWthdrawalRecordMapper.deleteDWthdrawalRecordById(id);
    }
}
